package nl.ViewerServer.ServerManager.DataManager.DataObjects;

import java.awt.Dimension;
import java.io.IOException;

import nl.ViewerServer.Exceptions.DataInitializationException;
import nl.ViewerServer.Exceptions.DataObjectException;


public abstract class DataObject {
   protected transient int    dataObjectId    = -1;
   protected transient int    lookmarkId      = -1;
   protected transient String location        = "";
   protected String           dataType        = "";
   protected Dimension[]      resolutionParts = new Dimension[ 0 ];
   protected Bounds           bounds          = new Bounds(0, 0, 0);
   protected int              tilesize        = 256;
   
   public DataObject( String dataType ) {
      this.dataType = dataType;
      this.resolutionParts = new Dimension[ 1 ];
      this.resolutionParts[0] = new Dimension( 1, 1 );
   }

   public Bounds getBounds() {
      return bounds;
   }

   public void setBounds( Bounds bounds ) {
      this.bounds = bounds;
   }

   public DataObject( int dataObjectId, int lookmarkId, String location, String dataType ) {
      this.dataObjectId = dataObjectId;
      this.lookmarkId = lookmarkId;
      this.location = location;
      this.resolutionParts = new Dimension[ 1 ];
      this.resolutionParts[0] = new Dimension( 1, 1 );
   }

   public int getDataObjectId() {
      return dataObjectId;
   }

   public void setDataObjectId( int dataObjectId ) {
      this.dataObjectId = dataObjectId;
   }

   public int getLookmarkId() {
      return lookmarkId;
   }

   public void setLookmarkId( int lookmarkId ) {
      this.lookmarkId = lookmarkId;
   }

   public String getLocation() {
      return location;
   }

   public void setLocation( String location ) {
      this.location = location;
   }

   public String getDataType() {
      return dataType;
   }

   public void setDataType( String dataType ) {
      this.dataType = dataType;
   }

   public int[] getPartCount( int resolution ) throws DataObjectException {
      if( resolution >= resolutionParts.length ) {
         throw new DataObjectException( "Resolution " + resolution + " is not available for this data object: " + this );
      }
      return new int[] { resolutionParts[resolution].width, resolutionParts[resolution].height };
   }

   public int getResolutionLevelCount() {
      return resolutionParts.length;
   }

   public abstract byte[] getPart( int resolution, int horizontalPart, int verticalPart ) throws DataObjectException,
         IOException;

   public abstract boolean initialize() throws DataInitializationException;

   public Dimension[] getResolutionParts() {
      return resolutionParts;
   }

   public boolean isSinglePart() {
      return( getResolutionLevelCount() == 1 && resolutionParts[0].height == 1 && resolutionParts[0].width == 1 );
   }

}
